<template>
  <!-- 搜索歌词 -->
  <div class="sc-lyric">
    <search-not-found v-if="!found"></search-not-found>
    <div v-if="found">
      <sc-song-list :songs="songs" :showLyric="true"></sc-song-list>
      <div class="songlist-el-pagination" v-if="songCount > 30">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="30"
          layout="prev, pager, next, jumper"
          :total="songCount"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import ScSongList from "views/search/child/ScSongList";
import SearchNotFound from "views/search/child/SearchNotFound";

export default {
  components: {
    ScSongList,
    SearchNotFound,
  },
  data() {
    return {
      songs: [],
      currentPage: 1,
      songCount: 0,
      found: true,
    };
  },
  mounted() {
    this.getSearchSong();
  },
  methods: {
    handleCurrentChange() {
      this.getSearchSong();
      document.documentElement.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    getSearchSong() {
      this.axios({
        url:
          "/cloudsearch?type=1006&keywords=" +
          this.$store.state.MInput +
          "&offset=" +
          (this.currentPage - 1) * 30,
      })
        .then((res) => {
          if (!res.data.result || res.data.result.songCount == 0) {
            this.found = false;
          } else {
            this.songs = res.data.result.songs;
            this.songCount = res.data.result.songCount;
          }
          console.log(this.songs);
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
.sc-lyric {
  margin-bottom: 50px;
  overflow: hidden;
}
.sc-lyric .songlist-el-pagination {
  position: relative;
  text-align: center;
  margin: 20px auto;
}
</style>